package com.txy.webmanagement.service;

import com.gitee.sunchenbin.mybatis.actable.command.PageResultCommand;
import com.gitee.sunchenbin.mybatis.actable.manager.common.BaseMysqlCRUDManager;
import com.txy.webmanagement.model.SysUserType;
import com.txy.webmanagement.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 用户类型
 *
 * @author wf
 * @date 2019-03-01$
 **/
@Service
public class SysUserTypeService {
    @Autowired
    private BaseMysqlCRUDManager baseMysqlCRUDManager;

    /**
     * 保存或者修改信息
     *
     * @param type
     * @return
     */
    public Boolean save(SysUserType type) {
        if (StringUtils.checkStringNotNull(type.getTypeName())) {
            if (StringUtils.checkStringNotNull(type.getUuid())) {
                SysUserType oldType = getOne(type.getUuid());
                if (oldType != null) {
                    oldType.setTypeName(type.getTypeName());
                    baseMysqlCRUDManager.save(oldType);
                    return true;
                } else {
                    return false;
                }
            } else {
                type.setUuid(StringUtils.getUUID());
                type.setCreateTime(new Date());
                type.setEnable(1);
                int cout = baseMysqlCRUDManager.save(type);
                if (cout > 0) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    }

    public SysUserType getOne(String uuid) {
        if (StringUtils.checkStringNotNull(uuid)) {
            SysUserType company = new SysUserType();
            company.setUuid(uuid);
            PageResultCommand<SysUserType> ss = baseMysqlCRUDManager.query(company);
            if (ss.getData() != null && ss.getData().size() > 0) {
                return ss.getData().get(0);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

}
